package top.broncho.lottery.ui.main

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import top.broncho.lottery.R
import top.broncho.lottery.databinding.LayoutMainItemBinding
import top.broncho.lottery.databinding.LayoutSsqItemBinding
import top.broncho.lottery.db.LotteryEntity
import top.broncho.lottery.util.LotteryDataBindingComponent

class MainAdapter(private val itemClickListener: (LotteryEntity) -> Unit) :
    PagedListAdapter<LotteryEntity, RecyclerView.ViewHolder>(diffCallback) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return ViewHolder(
            LayoutMainItemBinding.inflate(
                inflater,
                parent,
                false,
                LotteryDataBindingComponent()
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        getItem(position)?.let {
            (holder as ViewHolder).apply {
                bind(createOnClickListener(it), it)
                itemView.tag = it
            }
        }
    }

    private fun createOnClickListener(item: LotteryEntity?): View.OnClickListener {
        return View.OnClickListener {
            item?.run {
                itemClickListener.invoke(this)
            }
        }
    }

    class ViewHolder(private val binding: LayoutMainItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(listener: View.OnClickListener, item: LotteryEntity?) {
            item?.list?.forEach {
                val itemBinding = DataBindingUtil.inflate<LayoutSsqItemBinding>(
                    LayoutInflater.from(binding.root.context),
                    R.layout.layout_ssq_item,
                    null,
                    false
                ).apply {
                    list = it
                }
                binding.currentContainer?.addView(itemBinding.root)
            }
            binding.apply {
                clickListener = listener
                lottery = item
                executePendingBindings()
            }
        }
    }

    companion object {
        /**
         * This diff callback informs the PagedListAdapter how to compute list differences when new
         * PagedLists arrive.
         * <p>
         * When you add a Cheese with the 'Add' button, the PagedListAdapter uses diffCallback to
         * detect there's only a single item difference from before, so it only needs to animate and
         * rebind a single view.
         *
         * @see androidx.recyclerview.widget.DiffUtil
         */
        private val diffCallback = object : DiffUtil.ItemCallback<LotteryEntity>() {
            override fun areItemsTheSame(oldItem: LotteryEntity, newItem: LotteryEntity): Boolean =
                oldItem._id == newItem._id

            /**
             * Note that in kotlin, == checking on data classes compares all contents, but in Java,
             * typically you'll implement Object#equals, and use it to compare object contents.
             */
            override fun areContentsTheSame(
                oldItem: LotteryEntity,
                newItem: LotteryEntity
            ): Boolean =
                oldItem.path == newItem.path
        }
    }
}